package test230530;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/5/30 20:58
 * 64. 最小路径和
 * https://leetcode.cn/problems/minimum-path-sum/
 */
public class Solution05 {

    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        // dp[i][j] 表示到达 i j 位置最小路径和
        int[][] dp = new int[m + 1][n + 1];
        // 初始化状态量
        for (int i = 0; i <= n; ++i) {
            dp[0][i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i <= m; ++i) {
            dp[i][0] = Integer.MAX_VALUE;
        }
        dp[0][1] = dp[1][0] = 0;
        // 动态规划迭代过程
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i-1][j-1];
            }
        }
        return dp[m][n];
    }
}
